package aole.mindfx;

import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.GridPane;
import aole.mindfx.model.MindFXAttribute;
import aole.mindfx.profile.Profiler;

public class EditAttributePane extends GridPane {

	private Label				label;
	private MindFXAttribute		fxattribute;
	private ComboBox< String >	cbTypes;
	private ComboBox< String >	cbScope;
	private TextField			txtDefault;
	private ComboBox< Integer >	cbPointers;

	class TypeEventHandler implements EventHandler< ActionEvent > {
		@Override
		public void handle( ActionEvent event ) {
			fxattribute.setType( cbTypes.getValue() );
			label.setText( fxattribute.getFullCaption() );
		}
	}

	class PointerEventHandler implements EventHandler< ActionEvent > {
		@Override
		public void handle( ActionEvent event ) {
			fxattribute.setPointers( cbPointers.getValue() );
			label.setText( fxattribute.getFullCaption() );
		}
	}

	class ScopeEventHandler implements EventHandler< ActionEvent > {
		@Override
		public void handle( ActionEvent event ) {
			fxattribute.setScope( cbScope.getValue() );
		}
	}

	class DefaultEventHandler implements EventHandler< KeyEvent > {
		@Override
		public void handle( KeyEvent event ) {
			fxattribute.setDefault( txtDefault.getText() );
		}
	}

	class DeleteEventHandler implements EventHandler< ActionEvent > {
		@Override
		public void handle( ActionEvent event ) {
			fxattribute.getParent().deleteFXAttribute( fxattribute );
		}
	}

	public EditAttributePane( Profiler profiler ) {
		super();
		setPadding( new Insets( 12, 12, 12, 12 ) );
		setHgap( 5 );
		setVgap( 5 );

		int row = 1;
		// attribute label
		label = new Label( "attribute AttributeName" );
		label.setStyle( MindFXAttribute.getStyle() );
		label.setFont( MindFXAttribute.getFont() );
		add( label, 1, row, 2, 1 );
		row++;

		// delete button
		Button b = new Button("Delete");
		b.setOnAction( new DeleteEventHandler() );
		add(b, 2, row, 1,1);
		row++;
		
		// attribute type
		Label type = new Label( "Type" );
		add( type, 1, row, 1, 1 );
		cbTypes = new ComboBox< String >();
		cbTypes.setItems( profiler.getAttributeTypes() );
		cbTypes.setOnAction( new TypeEventHandler() );
		add( cbTypes, 2, row, 1, 1 );
		row++;

		// attribute scope
		Label scope = new Label( "Scope" );
		add( scope, 1, row, 1, 1 );
		cbScope = new ComboBox< String >();
		cbScope.setItems( profiler.getAttributeScopes() );
		cbScope.setOnAction( new ScopeEventHandler() );
		add( cbScope, 2, row, 1, 1 );
		row++;

		// pointers
		Label l = new Label( "Pointers" );
		add( l, 1, row, 1, 1 );
		cbPointers = new ComboBox< Integer >();
		cbPointers.setItems( FXCollections.observableArrayList( 0, 1, 2 ) );
		cbPointers.setOnAction( new PointerEventHandler() );
		add( cbPointers, 2, row, 1, 1 );
		row++;

		// default value
		Label def = new Label( "Default" );
		add( def, 1, row, 1, 1 );
		txtDefault = new TextField();
		txtDefault.setOnKeyReleased( new DefaultEventHandler() );
		add( txtDefault, 2, row, 1, 1 );
		row++;
	}

	public void setAttribute( MindFXAttribute a ) {
		fxattribute = a;
		label.setText( a.getFullCaption() );
		cbTypes.setValue( a.getType() );
		cbScope.setValue( a.getScope() );
		cbPointers.setValue( a.getPointers() );
		txtDefault.setText( a.getDefault() );
	}

}
